package ro.peoplecentric.ctc.faces.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import ro.peoplecentric.ctc.common.entity.Item;
import ro.peoplecentric.ctc.common.exception.ApplicationException;
import ro.peoplecentric.ctc.common.exception.ConcurrentModificationException;
import ro.peoplecentric.ctc.common.service.ItemService;
import ro.peoplecentric.ctc.common.util.ui.ItemCriteria;

/**
 * Item list model bean.
 *
 * @author pguzun
 */
@Controller
@Scope("view")
public class ItemListBean extends BaseModelBean {
    
    protected static final long serialVersionUID = 13L;

    @Resource
    private ItemService itemService;
    //
    private Item currentObject;
    private ItemCriteria itemCriteria;
    //
    private List<Item> quickItems;
    private List<Item> items;
    private List<Item> selectedItems;

    public ItemListBean() {
    }

    @SuppressWarnings("unused")
    @PostConstruct
    private void init() {
        selectedItems = new ArrayList<>();
        itemCriteria = new ItemCriteria();
        try {
            quickItems = itemService.getLastModifiedItems();
            items = itemService.getAll();
        } catch (ApplicationException e) {
            getLogger().error(e.getMessage(), e);
            getMessagePublisher().addErrorMessage("msg.general.load.fail");
        }
    }

    /**
     * Delete action.
     */
    public void delete() {
        try {
            try {
                itemService.deleteItem(currentObject);
                getMessagePublisher().addInfoMessage("itemListPage.delete.succes");
            } catch (ConcurrentModificationException e) {
                getMessagePublisher().addWarningMessage("itemListPage.delete.fail.concurent", new String[]{currentObject.getTitle()});
            }
            // update index in collections for ajax view scope request
            quickItems.remove(currentObject);
            items.remove(currentObject);
            selectedItems.remove(currentObject);
            // null the reference
            currentObject = null;
        } catch (ApplicationException e) {
            getLogger().error(e.getMessage(), e);
            getMessagePublisher().addErrorMessage("msg.general.fail");
        }
    }

    /**
     * Delete multiple action.
     */
    public void deleteSelectedItems() {
        // each item has it's own transaction
        for (Iterator<Item> it = selectedItems.iterator(); it.hasNext();) {
            Item item = it.next();
            try {
                try {
                    itemService.deleteItem(item);
                } catch (ConcurrentModificationException e) {
                    getMessagePublisher().addWarningMessage("itemListPage.delete.fail.concurent", new String[]{item.getTitle()});
                }
                // update index in collections for ajax view scope request
                quickItems.remove(item);
                items.remove(item);
                it.remove();
            } catch (ApplicationException e) {
                getLogger().error(e.getMessage(), e);
                getMessagePublisher().addErrorMessage("itemListPage.delete.fail.detail", new String[]{item.getTitle()});
            }
        }

        if (selectedItems.isEmpty()) {
            getMessagePublisher().addInfoMessage("itemListPage.delete.successes");
        } else {
            getMessagePublisher().addWarningMessage("items-form:top-message", "itemListPage.delete.partial.succes");
        }
    }

    /**
     * Search action.
     */
    public void search() {
        try {
            items = itemService.find(itemCriteria);
        } catch (ApplicationException e) {
            getLogger().error(e.getMessage(), e);
            getMessagePublisher().addErrorMessage("msg.general.fail");
        }
    }

    /**
     * Select action.
     */
    public void triggerSelect(Item item) {
        if (selectedItems.contains(item)) {
            selectedItems.remove(item);
        } else {
            selectedItems.add(item);
        }
    }

    /**
     * @return the items
     */
    public List<Item> getItems() {
        return items;
    }

    /**
     * @return the quickItems
     */
    public List<Item> getQuickItems() {
        return quickItems;
    }

    /**
     * @return the currentObject
     */
    public Item getCurrentObject() {
        return currentObject;
    }

    /**
     * @param currentObject the currentObject to set
     */
    public void setCurrentObject(Item currentObject) {
        this.currentObject = currentObject;
    }

    /**
     * @return the itemCriteria
     */
    public ItemCriteria getItemCriteria() {
        return itemCriteria;
    }

    /**
     * @return the selectedItems
     */
    public List<Item> getSelectedItems() {
        return selectedItems;
    }
}
